package cn.wangbencun.qrcode.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import cn.wangbencun.qrcode.R;

public class MainActivity extends Activity {
    private SlidingMenu slidingMenu;
    private Context context;
    private RadioGroup radioGroup;
    private RadioButton radioButton[] = null;
    private String[] arrTitle = null;
    private PopupWindow popupWindow;
    private ImageView imageView_menu;
    private View popupView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context =this;
        initData();
        initView();
        initSlidingMenu();
        initPopupWindow();
    }

    private void initPopupWindow() {
        popupView = getLayoutInflater().inflate(R.layout.menu_layout_popup_main,null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2524ff")));
        //z
        ListView listView_popup = (ListView) popupView.findViewById(R.id.listView_popupwindow);
        String arrMenu[] =  getResources().getStringArray(R.array.arrPopupmenu_main);
    }

    private void initData() {
        arrTitle = getResources().getStringArray(R.array.arrRadioGroup);
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_main);
        imageView_menu = (ImageView) findViewById(R.id.imageView_choose);
        if(arrTitle!=null){
            int length = arrTitle.length;
            radioButton = new RadioButton[length];
            int width = getResources().getDisplayMetrics().widthPixels/length;
            for(int i=0;i<length;i++){
                radioButton[i] = new RadioButton(context);
                radioButton[i].setText(arrTitle[i]);
//                radioButton[i].setTextSize(25);
//                radioButton[i].setTextColor(Color.parseColor("#ffff0000"));
                radioButton[i].setWidth(width);
                radioButton[i].setGravity(Gravity.CENTER);
                radioButton[i].setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
                radioButton[i].setBackgroundResource(R.drawable.bg_radiobutton);
                radioGroup.addView(radioButton[i]);
            }
            radioButton[0].setChecked(true);
        }
    }

    private void initSlidingMenu() {
        slidingMenu = new SlidingMenu(context);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setMenu(R.layout.menu_sliding_user);
        int width= getResources().getDisplayMetrics().widthPixels;
        int it = width * 4 / 5;
        slidingMenu.setBehindWidth(it);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
    }

    @Override
    protected void onStart() {
        super.onStart();
        overridePendingTransition(R.anim.mainactivity_in, R.anim.splist_out);
    }
    public void click(View view){
        switch (view.getId()){
            case R.id.imageView_user:
                slidingMenu.toggle();
                break;
            case R.id.imageView_choose:
//                popupWindow.setAnimationStyle(R.style.popupwindow);
                popupWindow.showAsDropDown(imageView_menu);
                break;
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int id = event.getKeyCode();
        switch (id){
            case KeyEvent.KEYCODE_BACK:
                if(popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                break;
            case KeyEvent.KEYCODE_MENU:
                if(!popupWindow.isShowing()){
//                    popupWindow.setAnimationStyle(R.style.popupwindow);
                    popupWindow.showAsDropDown(imageView_menu);

                }else{
                    popupWindow.dismiss();
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

}
